package hzncc.wcs.modbus;

import com.intelligt.modbus.jlibmodbus.Modbus;
import com.intelligt.modbus.jlibmodbus.master.ModbusMaster;
import com.intelligt.modbus.jlibmodbus.master.ModbusMasterFactory;
import com.intelligt.modbus.jlibmodbus.tcp.TcpParameters;
import hzncc.wcs.mqtt.MqttGateway;
import jakarta.annotation.Resource;

import java.net.InetAddress;
import java.util.Random;
import java.util.stream.IntStream;

public class ModbusMasterExample {
    //读线程的标识,在业务需要时可以控制程序的状态
    public static boolean FLAG = true;
    //读间隔时间
    public static final long READ_SPACE = 10000;
    public static final String IP = "127.0.0.1";
    //从机地址
    public static final int SLAVE_ID = 1;


    public static void main(String[] args) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    //设置主机TCP参数
                    TcpParameters tcpParameters = new TcpParameters();
                    //设置TCP的IP地址
                    InetAddress inetAddress = InetAddress.getByName(IP);
                    //TCP参数设置IP地址
                    tcpParameters.setHost(inetAddress);
                    //TCP设置长连接
                    tcpParameters.setKeepAlive(true);
                    //tcp设置端口,这里使用Modbus的默认端口502
                    tcpParameters.setPort(Modbus.TCP_PORT);
                    //创建一个主机
                    ModbusMaster modbusMaster = ModbusMasterFactory.createModbusMasterTCP(tcpParameters);
                    //Modbus.setLogLevel(Modbus.LogLevel.LEVEL_DEBUG);
                    //设置自增的id
                    Modbus.setAutoIncrementTransactionId(true);

                    if (!modbusMaster.isConnected()) {
                        System.out.println("Modbus主机没有连接,现在建立连接");
                        modbusMaster.connect();
                    } else {
                        System.out.println("Modbus主机已经连接");
                    }

                    while (FLAG) {
                        //寄存器读取开始地址
                        int offset = 0;
                        //读取寄存器数量
                        int quantity = 10;

                        //03
                        int[] registers = modbusMaster.readHoldingRegisters(SLAVE_ID, offset, quantity);

                        for (int register : registers) {
                            System.out.println("读取寄存器数据:" + offset++ + "-" + register);
//                            System.out.println("二进制："+ Integer.toBinaryString(register));
//                            System.out.println("mqtt消息已发送");
                            //修改寄存器的值
//                            modbusMaster.writeSingleRegister(SLAVE_ID, 1, Integer.parseInt(3,));
                        }
                        System.out.println("--------------------------------------------");
                        Thread.sleep(READ_SPACE);
                    }
                } catch (Exception e) {
                    System.out.println("jlibmodbus运行异常:" + e.getClass().getName() + "-->" + e.getMessage());
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
